package com.zlx.rulemk.marketing.buffer;

import com.zlx.rulemk.marketing.beans.BufferData;
import java.util.Map;
import com.zlx.rulemk.marketing.utils.ConnectionUtils;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;

/**
 * @Author: zhanglingxing
 * @CreateTime: 2022-07-09
 * @Description: 缓存管理工具
 * @Version: 1.0
 */
@Slf4j
public class BufferManagerImpl  implements BufferManager {

    Jedis jedis;

    public BufferManagerImpl(){
        jedis = ConnectionUtils.getRedisConnection();
    }
    @Override
    public BufferData getDataFromBuffer(String bufferKey) {
        Map<String, String> valueMap = jedis.hgetAll(bufferKey);
        String[] split = bufferKey.split(":");
        return new BufferData(split[0],split[1],valueMap);
    }
    @Override
    public boolean putDataToBuffer(BufferData bufferData) {
        String hmset = jedis.hmset(bufferData.getKeyByValue() + ":" + bufferData.getCacheId(),
                bufferData.getValueMap());
        return "OK".equals(hmset);
    }
    @Override
    public boolean putDataToBuffer(String bufferKey, Map<String,String> valueMap) {
        String hmset = jedis.hmset(bufferKey, valueMap);
        return "OK".equals(hmset);
    }
    @Override
    public void delBufferEntry(String bufferKey, String key) {
        jedis.hdel(bufferKey,key);

    }
}
